package com.uenpay.im.ui.old.activity.search;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qyx.android.message.type.PacketType;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-2-2 下午4:38:00
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class SearchResultAdapter extends BaseAdapter {

	private ArrayList<QYXUserEntity> arrayList = new ArrayList<QYXUserEntity>();
	private LayoutInflater mLayoutInflater = null;

	public SearchResultAdapter(Context _Context,
			ArrayList<QYXUserEntity> _ArrayList) {
		mLayoutInflater = LayoutInflater.from(_Context);
		arrayList = _ArrayList;
	}

	public void addData(ArrayList<QYXUserEntity> _ArrayList) {
		arrayList.addAll(_ArrayList);
		notifyDataSetChanged();
	}

	public void setData(ArrayList<QYXUserEntity> _ArrayList) {
		arrayList = _ArrayList;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return arrayList == null ? 0 : arrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arrayList == null ? null : arrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		SearchResultViewHodler hodler = null;
		QYXUserEntity mQyxUserEntity = arrayList.get(arg0);
		if (arg1 == null) {
			hodler = new SearchResultViewHodler();
			arg1 = mLayoutInflater.inflate(R.layout.item_search_result_layout,
					null);
			hodler.nameTv = (TextView) arg1.findViewById(R.id.nameTv);
			hodler.areaTv = (TextView) arg1.findViewById(R.id.areaTv);
			hodler.sexIv = (ImageView) arg1.findViewById(R.id.sexIv);
			hodler.avatarIv = (RoundImageView) arg1.findViewById(R.id.avatar);
			arg1.setTag(hodler);
		} else {
			hodler = (SearchResultViewHodler) arg1.getTag();
		}
		hodler.nameTv.setText(mQyxUserEntity.nickname);
		hodler.areaTv.setText(mQyxUserEntity.area);
		hodler.avatarIv.SetUrl(APIConfiguration.getAvatarUrlNormal(
				mQyxUserEntity.custid, PacketType.PERSONAL));
		if (mQyxUserEntity.sex == 0) {
			hodler.sexIv.setImageResource(R.drawable.icon_woman);
		} else {
			hodler.sexIv.setImageResource(R.drawable.icon_man);
		}
		return arg1;
	}

	private class SearchResultViewHodler {
		private TextView nameTv, areaTv;
		private ImageView sexIv;
		private RoundImageView avatarIv;
	}
}
